﻿using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;

namespace Hutech.Authentication.Library.Data
{
    public static class DBRolePermission
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString_CustommerService27032013"];
        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {
            if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString_CustommerService27032013"] != null)
            {
                return ConfigurationManager.AppSettings["MSSQLWriteConnectionString_CustommerService27032013"];
            }

            return ConfigurationManager.AppSettings["MSSQLConnectionString_CustommerService27032013"];
        }


        /// <summary>
        /// Inserts a row in the root_RolePermission table. Returns rows affected count.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        /// <param name="editDate"> editDate </param>
        /// <param name="editUserID"> editUserID </param>
        /// <returns>int</returns>
        public static int Create(
            Guid roleId,
            Guid actionGuid,
            Guid rolePermissionGuid,
            DateTime editDate,
            Guid editUserID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_RolePermission_Insert", 5);
            sph.DefineSqlParameter("@RoleId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, roleId);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            sph.DefineSqlParameter("@RolePermissionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, rolePermissionGuid);
            sph.DefineSqlParameter("@EditDate", SqlDbType.DateTime, ParameterDirection.Input, editDate);
            sph.DefineSqlParameter("@EditUserID", SqlDbType.UniqueIdentifier, ParameterDirection.Input, editUserID);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the root_RolePermission table. Returns true if row updated.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        /// <param name="editDate"> editDate </param>
        /// <param name="editUserID"> editUserID </param>
        /// <returns>bool</returns>
        public static bool Update(
            Guid roleId, Guid actionGuid, Guid rolePermissionGuid,
            DateTime editDate,
            Guid editUserID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_RolePermission_Update", 5);
            sph.DefineSqlParameter("@RoleId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, roleId);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            sph.DefineSqlParameter("@RolePermissionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, rolePermissionGuid);
            sph.DefineSqlParameter("@EditDate", SqlDbType.DateTime, ParameterDirection.Input, editDate);
            sph.DefineSqlParameter("@EditUserID", SqlDbType.UniqueIdentifier, ParameterDirection.Input, editUserID);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the root_RolePermission table. Returns true if row deleted.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(Guid rolePermissionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_RolePermission_Delete", 1);
            //sph.DefineSqlParameter("@RoleId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, roleId);
            //sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            sph.DefineSqlParameter("@RolePermissionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, rolePermissionGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the root_RolePermission table.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        public static IDataReader GetOne(
            Guid roleId, Guid actionGuid, Guid rolePermissionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_RolePermission_SelectOne", 3);
            sph.DefineSqlParameter("@RoleId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, roleId);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            sph.DefineSqlParameter("@RolePermissionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, rolePermissionGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets an IDataReader with one row from the root_RolePermission table.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        public static IDataReader GetOneBy2Para(Guid roleId, Guid actionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_RolePermission_SelectOneBy2Para", 2);
            sph.DefineSqlParameter("@RoleId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, roleId);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the root_RolePermission table.
        /// </summary>
        public static int GetCount()
        {

            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "root_RolePermission_GetCount",
                null));

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the root_RolePermission table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "root_RolePermission_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the root_RolePermission table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount();

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_RolePermission_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();

        }

        #region add by tsandtm
        internal static IDataReader GetAllByRoleID(Guid RoleID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_RolePermission_GetAllByRoleID", 1);
            sph.DefineSqlParameter("@RoleID", SqlDbType.UniqueIdentifier, ParameterDirection.Input, RoleID);
            return sph.ExecuteReader();
        }
        #endregion

        #region add by tsandtm
        internal static bool CheckPermission(Guid RoleID, string ActionCommand)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_RolePermission_CheckPermission", 2);
            sph.DefineSqlParameter("@RoleID", SqlDbType.UniqueIdentifier, ParameterDirection.Input, RoleID);
            sph.DefineSqlParameter("@ActionCommand", SqlDbType.NVarChar, 256, ParameterDirection.Input, ActionCommand);
            int rowsAffected = Convert.ToInt32(sph.ExecuteScalar());
            return (rowsAffected > 0);
        }
        #endregion
    }

}

